//
//  DFVisitorView.swift
//  FlyBlog
//
//  Created by FEI on 16/4/8.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit

protocol DFVisitorViewDelegate:NSObjectProtocol {
    func visitorViewDidClickRegisterButton()
    func visitorViewDidClickLoginButton()
}

class DFVisitorView: UIView {

    weak var delegate:DFVisitorViewDelegate?

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        prepareUI()

        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    }

    //MARK:visitor视图 方法
    func setupVisitorViewInfo(imageName:String , message:String) {
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        homeView.hidden = true
        coverView.hidden = true
    }


    private func prepareUI() {
        //添加控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loadButton)


        //禁用autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registButton.translatesAutoresizingMaskIntoConstraints = false
        loadButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false


        //添加约束

        //转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))

        //遮罩
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

        //消息
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant:16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))


        //注册按钮
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))

        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))

        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

        //登录按钮
        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))

        addConstraint(NSLayoutConstraint(item: loadButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))





    }

    //MARK:按钮点击事件
    @objc private func didClickRegisterButton() {
        delegate?.visitorViewDidClickRegisterButton()

    }

    @objc private func didClickLoginButton() {
        delegate?.visitorViewDidClickLoginButton()
    }

//MARK: 旋转动画
    func iconViewRotateAnimation() {
        let rotate = CABasicAnimation(keyPath: "transform.rotation")
        rotate.toValue = 2 * M_PI
        rotate.duration = 20
        rotate.repeatCount = MAXFLOAT
        rotate.removedOnCompletion = false
        iconView.layer.addAnimation(rotate, forKey: "homeRotation")

    }


//MARK: -- 懒加载控件
    //转轮
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    //遮罩
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //房子
    private lazy var homeView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    //消息文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()

        //设置
        label.text = "关注一些人!看看有什么惊喜!"
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center

        return label
    }()

    //注册按钮
    private lazy var registButton : UIButton = {

        let button = UIButton ()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("didClickRegisterButton"), forControlEvents: UIControlEvents.TouchUpInside)
        return button

    }()

    //登录按钮
    private lazy var loadButton : UIButton = {

        let button = UIButton ()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("didClickLoginButton"), forControlEvents: UIControlEvents.TouchUpInside)
        return button

    }()




}
